package cn.school.student.service.impl;

import cn.school.common.utils.StringUtils;
import cn.school.common.utils.SysUserUtils;
import cn.school.student.dao.StudentGuardianDao;
import cn.school.student.dao.StudentInfoDao;
import cn.school.student.entity.StudentGuardian;
import cn.school.student.entity.StudentInfo;
import cn.school.student.service.StudentGuardianService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: lx
 * @date: 2024/3/22 9:11
 * @description:
 */
@Service
public class StudentGuardianServiceImpl extends ServiceImpl<StudentGuardianDao, StudentGuardian>
        implements StudentGuardianService {

    @Autowired
    StudentGuardianDao guardianDao;

    @Autowired
    StudentInfoDao studentInfoDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addStudentGuardian(List<StudentGuardian> guardianList) {
        //1.限制可添加的最大数量
        if(guardianList.size() > 3){
            return -1;
        }
//        Long studentId = SysUserUtils.getParentUser().getId();
        Long studentId = null;
        if(guardianList.size() > 0){
//            if(!guardianList.get(0).getStudentId().equals(studentId)){
//                return -2;
//            }
            studentId = guardianList.get(0).getStudentId();
        }else {
            return 9;
        }

        StudentInfo studentInfo = studentInfoDao.selectById(studentId);
        if(studentInfo == null){
            return -3;
        }

        //2.查询当前db中的数据
        LambdaQueryWrapper<StudentGuardian> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudentGuardian::getStudentId,studentId);
        List<StudentGuardian> dbGuardianList = guardianDao.selectList(wrapper);
        //3.传入数据和db中数据匹配的，如果存在openid 则将openid保留至新的list集合
        for(StudentGuardian item : dbGuardianList){
            for(int i = 0 ; i < guardianList.size(); i++){
                if(item.getGuardianPhone().equals(guardianList.get(i).getGuardianPhone())){
                    if(!StringUtils.isEmpty(item.getOfficialOpenId())){
                        guardianList.get(i).setOfficialOpenId(item.getOfficialOpenId());
                    }
                }
            }
        }
        //4.插入新数据
        for(StudentGuardian item : guardianList){
            item.setStudentName(studentInfo.getName());
            item.setStudentGuardianPhone(studentInfo.getGuardianPhoneNumber());
            item.setTenantId(studentInfo.getTenantId());
            guardianDao.insert(item);
        }
        //5.删除旧数据
        for(StudentGuardian item : dbGuardianList) {
            guardianDao.deleteById(item.getId());
        }
        return 1;
    }

    @Override
    public List<StudentGuardian> getStudentGuardianList(Long studentId) {
        LambdaQueryWrapper<StudentGuardian> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudentGuardian::getStudentId,studentId);
        return guardianDao.selectList(wrapper);
    }
}
